package com.skydonkey.es;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ESVideoService {

    @Autowired
    private ESVideoRepository esVideoRepository;

    public Video saveOrUpdateVideo(Video video) {
        // 检查视频是否已存在，如果存在，则删除
        esVideoRepository.findById(video.getVideoId()).ifPresent(existingVideo -> esVideoRepository.delete(existingVideo));
        // 插入新的记录
        return esVideoRepository.save(video);
    }

    public Page<Video> searchByTitle(String title, int page, int size) {
        Page<Video> results = esVideoRepository.findByTitleUsingCustomQuery(title, PageRequest.of(page - 1, size));
        return results;
    }

}
